package com.debai.controller;

import com.debai.pojo.Order;
import com.debai.pojo.Position;
import com.debai.service.Impl.OrderServiceImpl;
import com.debai.service.OrderService;
import com.debai.service.PositionService;
import com.debai.service.ScreenService;
import com.debai.service.UserService;
import com.debai.util.PageBean;
import com.debai.util.Result;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private UserService userService;


    //添加订单信息
    @PostMapping("/insert")
    public Result insert(@RequestBody Order order, @RequestHeader(name = "Authorization") String token,String screenPosition) {
        int i=orderService.insert(order,token,screenPosition);
        if (i>=1){
            return Result.success();
        }
        return Result.error("插入失败");
    }

    //删除订单信息
    @DeleteMapping("/delete")
    public Result delete(@RequestParam(name = "id") int id) {
        int i=orderService.delete(id);
        if (i!=1){
            return Result.error("删除失败");
        }
        return Result.success();
    }


    @PutMapping("/update")
    public Result update(@RequestBody Order order,@RequestHeader(name = "Authorization") String token,@RequestParam(name = "screenPosition") String screenPosition) {
        int i=orderService.update(order,token,screenPosition);
        if (i!=1){
            return Result.error("更新失败");
        }
        return Result.success();
    }

    //管理权限
    //查看所有门店的总收入
    @GetMapping("/selectAllPosititionSumMoney")
    public Result<List<Order>> selectAllPosititionSumMoney(@RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                           @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
        System.out.println(startTime);
        System.out.println(endTime);
        List<Order> listOrder=orderService.selectAllPosititionSumMoney(startTime,endTime);
        return Result.success(listOrder);
    }

    //管理权限
    //查看单个门店详细收入信息
    @GetMapping("/selectOnePasition")
    public Result<List<Order>> selectOnePasition(@RequestParam(name = "positionId") int positionId,
                                                 @RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                 @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
        List<Order> order=orderService.selectOnePasition(positionId,startTime,endTime);
        return Result.success(order);
    }

    //门店用户的权限
    //查看单个门店总收入信息
    @GetMapping("/selectUserOfPasition")
    public Result<List<Order>> selectUserOfPasition(@RequestHeader(name = "Authorization") String token,
                                                 @RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                 @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
        List<Order> orders=orderService.selectUserOfPasition(token,startTime,endTime);
        return Result.success(orders);
    }

    //分页只能在分页之后的语句起作用
    //门店用户的权限
    //查看单个门店详细收入信息（分页）
    @GetMapping("/selectUserOfPasitionPageBean")
    public Result<PageBean<Order>> selectUserOfPasitionPageBean(int pageNum,int pageSize,@RequestHeader(name = "Authorization") String token,
                                                                @RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                                @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
//        List<Order> orders=orderService.selectUserOfPasition(token,startTime,endTime);

        PageBean<Order> orders=orderService.selectOnePasitionPageBean(pageNum,pageSize,token,startTime,endTime);
        return Result.success(orders);
    }

    //管理员功能
    //查询单个门店的详细订单信息
    @GetMapping("detailPasitionOfOrder")
    public Result<List<Order>> detailOfPasitionOrder(@RequestParam(name = "positionId") int positionId,
                                                     @RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                     @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
        List<Order> orders=orderService.detailOfPasitionOrder(positionId,startTime,endTime);
        return Result.success(orders);
    }

    //用户功能
    //查询单个门店的详细订单信息
    @GetMapping("detailUserOfPositionOrder")
    public Result<List<Order>> detailUserOfPositionOrder(@RequestHeader(name = "Authorization")String token,
                                                     @RequestParam(required=false)@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime startTime,
                                                     @RequestParam(required=false)@DateTimeFormat(pattern="yyyy-MM-dd")LocalDateTime endTime) {
        List<Order> orders=orderService.detailUserOfPositionOrder(token,startTime,endTime);
        return Result.success(orders);
    }
}
